﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Common;
using ScrumPilot.Domain.Models;
using ScrumPilot.TFSHelpers;

namespace ScrumPilot.Domain.BPL
{
    public class SourcesServices : BusinessProcessLayer
    {
        public SourcesServices(TFSServices tfs)
            : base(tfs)
        {
        }
        public CheckinModel GetCheckIn(Int64 id)
        {
            CheckinModel chki = null;
            using (var unitOfWork = new UnitOfWork())
            {
                spevent evt =unitOfWork.EventRepository.GetById(id);
                if (evt != null)
                {
                    var cev = TFS.DeserializeChangedEvent<CheckinEvent>(evt.eventobject);
                    if (cev != null)
                    {
                        int pos = cev.ContentTitle.IndexOf(':');
                        chki = new CheckinModel
                                   {
                                       AuthorID = evt.userid.GetValueOrDefault(),
                                       Title = pos > 0 ? cev.ContentTitle.Substring(0, pos) : cev.ContentTitle,
                                       Comments = cev.Comment,
                                       ActionDate = evt.createdate.ToString(CultureInfo.InvariantCulture),
                                       Author = evt.user.name,
                                       Artifacts = new List<Artifact>()
                                   };
                        foreach (ClientArtifact art in cev.Artifacts)
                        {
                            if (!String.IsNullOrEmpty(art.Item))
                            {
                                chki.Artifacts.Add(new Artifact
                                                       {
                                                           Name = art.Item + " (" + art.ServerItem + ")",
                                                           Url = art.ServerItem,
                                                           ChangedType = art.ChangeType
                                                       });
                            }
                        }
                    }
                }
                return chki;
            }
        }

        public List<CheckOutFile> GetCheckOutUser(Int32 id)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                try
                {
                    user usr = unitOfWork.UserRepositery.GetById(id);
                    var tf = new List<TFSWallContentObjectWithUser>();
                    List<String> follow = this.FollowedProjectName;
                    follow.ForEach(s => tf.AddRange(TFS.GetCheckOut(usr.tfsname, s)));
                    var lstRet = new List<CheckOutFile>(tf.Count);
                    tf.ForEach(tfo => lstRet.Add(new CheckOutFile
                    {
                        Id = tfo.Id,
                        Name = tfo.Name,
                        date = tfo.Date,
                        ProjectName = tfo.ProjectName
                    }));
                    return lstRet.OrderByDescending(p => p.date).ToList();
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

        public List<CheckOutFile> GetCheckOutProject(Int32 id)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                project prj = unitOfWork.ProjectRepository.GetById(id);
                List<TFSWallContentObjectWithUser> tf = TFS.GetCheckOut(prj.name);
                var lstRet = new List<CheckOutFile>(tf.Count);
                user usr;
                tf.ForEach(delegate(TFSWallContentObjectWithUser tfo)
                               {
                                   usr = unitOfWork.UserRepositery.Get(p=>p.name==tfo.Username);
                                   lstRet.Add(new CheckOutFile
                                                  {
                                                      Id = tfo.Id,
                                                      Name = tfo.Name,
                                                      date = tfo.Date,
                                                      OwnerName = usr != null ? usr.name : String.Empty,
                                                      OwnerID = usr != null ? usr.userid : (int?) null
                                                  });
                               });
                return lstRet.OrderByDescending(p => p.Date).ToList();
            }
        }
    }
}
